<template>
	<view class="container">
		<view class="content" :style="{ top: getStatusBarHeight() + 'px' }">
			<view class="left-panel">
				<scroll-view scroll-y show-scrollbar="false">
					<view
						class="scroll-item"
						v-for="(item, index) in industries"
						:key="item.industry.id"
						:class="['category-item', { active: selectedIndustry && selectedIndustry.industry.id === item.industry.id }]"
						@click="selectIndustry(item)"
					>
						{{ item.industry.name }}
					</view>
				</scroll-view>
			</view>
			<view class="right-panel">
				<view class="ranking-header">
					<text class="col rank">排名</text>
					<text class="col company">企业名称</text>
				</view>
				<scroll-view class="ranking-scroll" scroll-y show-scrollbar="false">
					<view v-if="loading" class="loading">加载中...</view>
					<view v-else>
						<view v-if="!selectedIndustry || selectedIndustry.rankings.length === 0" class="no-data">暂无数据</view>
						<view class="ranking-item" v-for="(item, index) in selectedIndustry?.rankings" @click="goEnterprise(item)" :key="item.enterprise.id">
							<text class="col rank">{{ item.rank }}</text>
							<text class="col company">{{ item.enterprise.name }}</text>
						</view>
					</view>
				</scroll-view>
			</view>
		</view>
	</view>
</template>

<script setup>
import { getStatusBarHeight } from '@/utils/system.js';
import { ref, onMounted, computed, watch } from 'vue';
import { getAllRanking } from '@/api/enterprise.js';
import { onLoad } from '@dcloudio/uni-app';

const industriesRankings = ref([]);
const industries = ref([]);
const selectedIndustry = ref(null);
const loading = ref(false);

onLoad(() => {
	getData();
});

// 选择行业时更新选中项
const selectIndustry = (item) => {
	selectedIndustry.value = item;
};

const getData = async () => {
	loading.value = true;
	const res = await getAllRanking();
	industriesRankings.value = res.data.industriesRankings || [];
	industries.value = industriesRankings.value;
	if (industries.value.length > 0) {
		selectedIndustry.value = industries.value[0];
	}
	loading.value = false;
};

const goEnterprise = (row) => {
	uni.navigateTo({
		url: `/pages/enterpriseDetail/enterpriseDetail?id=${row.enterprise.id}`
	});
};
</script>

<style lang="scss" scoped>
.container {
	position: relative;
	.content {
		padding: 30rpx;
		position: absolute;
		display: flex;
		.left-panel {
			width: 150rpx;
			display: flex;
			flex-direction: column;
			align-items: center;
			background-color: #f8f8f8;
			height: calc(100vh - 160rpx);
			overflow: auto;
			.scroll-item {
				height: 80rpx;
				font-size: 20rpx;
				line-height: 80rpx;
				text-align: center;
				border-bottom: 0.5rpx solid black;
				padding-bottom: 20rpx;
				white-space: nowrap;
				overflow: hidden;
				text-overflow: ellipsis;
			}
			.category-item.active {
				background-color: #ffffff;
				color: #289fde;
			}
		}
		.right-panel {
			flex: 1;
			padding-left: 30rpx;
			width: calc(100vw - 220rpx);
			background-color: #fff;
			.ranking-header {
				display: flex;
				justify-content: space-between;
				padding: 10rpx 0;
				border-bottom: 1px solid #ddd;
				font-weight: bold;
				font-size: 28rpx;
				.col {
					flex: 1;
					text-align: center;
				}
				.rank {
					flex: 0.5;
				}
				.company {
					flex: 1.5;
				}
			}
			.ranking-scroll {
				height: calc(100vh - 240rpx);
				overflow: auto;
				&::-webkit-scrollbar {
					display: none;
				}
				-ms-overflow-style: none;
				scrollbar-width: none;
			}
			.ranking-item {
				display: flex;
				justify-content: space-between;
				padding: 10rpx 0;
				border-bottom: 1px solid #eee;
				font-size: 28rpx;
				.col {
					flex: 1;
					text-align: center;
				}
				.rank {
					flex: 0.5;
				}
				.company {
					flex: 1.5;
					white-space: nowrap;
					overflow: hidden;
					text-overflow: ellipsis;
				}
			}
			.loading,
			.no-data {
				text-align: center;
				color: #999;
				padding: 20rpx 0;
				font-size: 28rpx;
			}
		}
	}
}
</style>
